package com.xiaoyu.izf.service;

import com.xiaoyu.izf.model.GoodsInfo;
import com.xiaoyu.izf.repository.GoodsInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

@Service
public class GoodsService extends BaseService{
    @Autowired
    private GoodsInfoDao goodsInfoDao;

    public GoodsInfo goodsOne(int id){
        return goodsInfoDao.findOneById(id);
    }

    public Page<GoodsInfo> listForApi(int p, int psize){
        PageRequest pageRequest = buildPageRequest(p, psize, "updateTime");
        Specification<GoodsInfo> specification = new Specification<GoodsInfo>() {
            @Override
            public Predicate toPredicate(Root<GoodsInfo> root,
                                         CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.equal(root.get("isDel"), 0);
                return predicate;
            }
        };
        Page<GoodsInfo> page = goodsInfoDao.findAll(specification,  pageRequest);
        return page;
    }
}
